package ua.kiev.tosik.price.modules.clients.gui;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import ua.kiev.tosik.price.gui.tools.ClipboardTools;
import ua.kiev.tosik.price.models.Clients;
import ua.kiev.tosik.price.modules.clients.RecordEditor;
import ua.kiev.tosik.price.modules.clients.models.ClientsTableModel;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

public class ClientsTable {

	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.gui");
	private JXTable table;
	private JScrollPane scrollPane;
	private ClientsTableModel model;

	public ClientsTable() {
		model = new ClientsTableModel(new Vector<Clients>());
		table = new JXTable(model);
		scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		table.setFillsViewportHeight(true);
		ListSelectionModel listMod = table.getSelectionModel();
		listMod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		table.addHighlighter(HighlighterFactory.createSimpleStriping());
		table.setColumnControlVisible(true);
		table.packAll();

		TableColumn tableColumn = table.getColumnModel().getColumn(0);
		tableColumn.setMinWidth(16);
		tableColumn.setMaxWidth(50);
		tableColumn.setPreferredWidth(45);

		TableColumn statusColumn = table.getColumnModel().getColumn(4);
		statusColumn.setMinWidth(16);
		statusColumn.setMaxWidth(50);
		statusColumn.setPreferredWidth(45);

		table.getColumnModel().getSelectionModel().addListSelectionListener(new ColumnListener());

		table.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int c = e.getKeyCode();
				if (c == KeyEvent.VK_ENTER) {
					e.consume();
					int row = table.convertRowIndexToModel(table.getSelectedRow());
					if (row >= 0) {
						editRecord(table.getSelectedRow());
					}
				}
				if (c == KeyEvent.VK_C) {
					e.consume();
					int rows[] = table.getSelectedRows();
					// int cell[] = table.getSelectedColumns();
					ClipboardTools tableTextContent = new ClipboardTools();
					StringBuilder sb = new StringBuilder();
					String delim = "\t";
					for ( int row: rows ) {
						Clients clients = getModel().getRow(table.convertRowIndexToModel(row));
						sb.append(clients.getName()); sb.append(delim);
						sb.append(clients.getPhone()); sb.append(delim);
						sb.append(clients.getEmail()); sb.append(delim);
						sb.append(clients.getHomePage()); sb.append(delim);
						sb.append(clients.getAddress()); sb.append(delim);
						sb.append(clients.getCreateDate()); sb.append("\n");
					}
					if ( sb.length() >= 0 ) {
						tableTextContent.setClipboardContents(sb.toString());
					}
				}
			}
		});

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
					int row = table.convertRowIndexToModel(table.getSelectedRow());
					if (row >= 0) {
						editRecord(table.getSelectedRow());
					}
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					// table.setEditingRow(table.rowAtPoint(e.getPoint()));
					// final int row = table.convertRowIndexToModel(table.getSelectedRow());
					// table.setEditingRow( table.rowAtPoint(e.getPoint()) ); // For JTable
					// contextMenu.show(e.getComponent(), e.getX(), e.getY());
				}
			}
		});

	}

	public JScrollPane getTable() {
		return scrollPane;
	}

	public ClientsTableModel getModel() {
		return model;
	}

	private void editRecord(int row) {
		Clients client = model.getRow(row);
		RecordEditor editor = new RecordEditor();
		editor.setClient(client);
		editor.setRowIndex(row);
		editor.editRecord();
	}

	private class ColumnListener implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			int selectedColumn = table.getSelectedColumn();
			// logger.debug("Select " + selectedColumn + " column");
		}
	}

}
